﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using System.Data;
using ClassLibrary.Entity;

namespace ClassLibrary.DAO
{
    public class TblCategoryDAO
    {
        private DBService _db = null;
        public TblCategoryDAO(String ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }
        public DataTable Category_InsUp(TblCategory cate, int mode)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("sp_TblCategory_InUp",
                    new string[] { "@IDCategory", "@CategoryName", "@mode" },
                    new object[] { cate.IDCategory, cate.CategoryName, mode }, CommandType.StoredProcedure);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public DataTable GetCate()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblCategory", false, CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public TblCategory GetCateByIDCate(int @IDCategory)
        {
            TblCategory cate = new TblCategory();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("select * from TblCategory where IDCategory = @IDCategory",
                    new string[] { "@IDCategory" },
                    new object[] { IDCategory }, CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    cate.IDCategory = Convert.ToInt32(dr["IDCategory"].ToString());
                    cate.CategoryName = dr["CategoryName"].ToString();       
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return cate;
        }


        public DataTable GetCate_ByID(int IDCate)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblCategory where IDCategory = @IDCate ",                     
                    new string[]{ "@IDCate" }, 
                    new object[]{ IDCate },CommandType.Text);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
